export const uniStorage = {
	/**
	 * 获取缓存
	 * @param key 
	 * @returns 
	 */
	getValue: function(key) {
		let keys = key.toUpperCase();
		let obj = uni.getStorageSync(keys)
		if (!obj) return null;
		let val = JSON.parse(obj)
		if (!val) return null;
		try {
			const {
				expire,
				value
			} = val
			if (!expire || expire > new Date().getTime()) {
				return value;
			}
			this.clear(key);
			return null;
		} catch (e) {
			this.clear(key);
			return null;
		}
	},

	/**
	 * 存储缓存
	 * @param key 
	 * @param value 
	 * @param expire 过期时间 秒值 如 5分钟  5 * 60
	 */
	save: function(key, value, expire = null) {
		let keys = key.toUpperCase();
		let stringData = JSON.stringify({
			nowTime: Date.now(),
			expire: expire ? new Date().getTime() + expire * 1000 : null,
			value
		})
		uni.setStorageSync(keys, stringData)
	},

	/**
	 * 清除缓存
	 * @param key 
	 */
	clear: function(key) {
		let keys = key.toUpperCase();
		uni.removeStorageSync(keys)
	}
}